/* 
 * Encapsulation Pattern: Conjuring YUI 3.x from thin air
 */
YUI_config = function(){
  /* injecting the YUI3 baseline in the current page */	
  var s = document.createElement('script');
  s.setAttribute('type','text/javascript');
  s.setAttribute('src','http://yui.yahooapis.com/3.0.0pr2/build/yui/yui-min.js');
  document.getElementsByTagName('head')[0].appendChild(s);
  return{
    listener:function(o){
      if ((typeof YUI === 'undefined') || !YUI || YUI.Loader) {
		    // keep waiting...
		    window.setTimeout(YUI_config.listener,1);
	    } else {	  
	      // YUI is ready...
		    window.setTimeout(YUI_config.ready,1);
      }
    },
    ready:function(){
      //debugging
      document.body.innerHTML += '<p>Bootstrap: starting the loading process...</p>';
      //debugging
      document.body.innerHTML += '<p>Bootstrap: Using YUI to load the required modules...</p>';
		  YUI({
		    charset: 'utf-8', // specify a charset for inserted nodes, default is utf-8
		    loadOptional: true, // automatically load optional dependencies, default false
		    combine: true, // use the Yahoo! CDN combo service for YUI resources, default is true unless 'base' has been changed
		    timeout: 10000, // specify the amount of time to wait for a node to finish loading before aborting
		    modules:  { // one or more external modules that can be loaded along side of YUI
			    
			    'myapp': {
				    fullpath: "js/app.js",
				    requires: ['get']
			    },
			    'simplemodule': {
				    fullpath: "js/simple-module.js",
				    requires: ['myapp', 'io', 'json']
			    }
		    }
		  }).use('simplemodule', 'myapp', function(Y) {
		  //}).use('ibrpg', function(Y) {  
			  /* loading the required components onDemand */
        /** **/
              var _myComponents = [
                "js/simple-component.js", 
                "js/another-component.js"
                ];
        /** **/        
        //debugging
        document.body.innerHTML += '<p>Bootstrap: Using Get Utility to load the rest of the components...</p>';
			  
			  /** **/
			  Y.Get.script(_myComponents, { 
				  onSuccess: function() { 
              // We are ready to roll.
              //debugging
              document.body.innerHTML += '<p>Bootstrap: We are ready to roll on top of YUI 3.x</p>'; 
            }}); 
		    /** **/
		    
		    });

		
        
    }
  };
}();
/* Hack: workaround for pr1 */
/*       - we don't have control over the loading process for the yui-min.js file, instead we check periodically until the script become available */
YUI_config.listener ();